 updateradiusbuttons=
{
  ctrlSetText [10904, format ["Radius %1 >",radius]];
  ctrlSetText [10400, format ["Del (%1m)",radius]];
  ctrlSetText [10398, format ["Fill interiors with current side (%1m)",radius]];
  ctrlSetText [10044, format ["Destroy buildings (%1m)",radius]];
  ctrlSetText [10045, format ["Kill everything (%1m)",radius]];
  _r=round(radius/10); if (_r<1) then {_r=1;};
  ctrlSetText [10049, format ["Artillery : %1 shells",_r,radius]];
};

vts_waitloadingclassinfo={

  while {isnil "GlobalSoundList"} do
  {
  taskhint ["Loading...\nAll The Things", [1, 0, 0, 1], "taskcurrent"];
  sleep 4;
  };
  taskhint ["GAME DATA\nLOADED", [0, 1, 0, 1], "taskcurrent"];
};

vts_getisGM=
{
  _object=_this select 0;
  _isDM=false;
  
  //GM unit
  if (_object==user1)  then {_isDM=true;};
  
  //GM in a control of another unit
  _gmable=_object getvariable "GMABLE";
  if ((_object!=user1) and !(isnil "_gmable")) then {_isDM=true;};

  //Debug test as player
  //_isDM=false;  
  
  _isDM;
};

vts_destroyterrain=
{
    _pos=_this select 0;
    _rad=_this select 1;
    _sync=_this select 2;
    _objs=nearestObjects [_pos,[],_rad];
    {
       if (!(_x isKindOf "AllVehicles") && !(_x isKindof "ReammoBox")) then 
       {
        
        if ((damage _x)<0.5) then
        {        
          if (typeof _x=="") then {_x setdamage 1;} else {_x setdamage 0.5;};
          
          for "_i" from 1 to 7 do 
          {
    			_x sethit [format ["dam%1",_i],1];
    			_x sethit [format ["dam %1",_i],1];
    			};
    		};
			 };
		    
    if ((isserver) && (round(random 10)==10)) then {_crater = "CraterLong" createVehicle (position _x);_crater setdir (random 360);};
    } foreach _objs;
    
    if (isserver && _sync) then 
    {
      if (isnil "syncdestroyed") then {syncdestroyed=[];};
      syncdestroyed=syncdestroyed+[[_pos,_rad]];publicvariable "syncdestroyed";
    };
};				

vts_syncdestroyedterrain=
{
  if (isnil "syncdestroyed") exitwith {};
  _num=(count syncdestroyed)-1;
  
  for [{_x=0},{_x<=_num},{_x=_x+1}] do
  {
    _boom=syncdestroyed select _x;
    _pos=_boom select 0;
    _rad=_boom select 1;
    [_pos,_rad,false] call vts_destroyterrain;
  };

};

vts_radiuschange=
{
_radius=0;
_incr=_this select 0;
//Less
if (_incr==0) then
{
  switch (radius) do
  {
  case 5:{_radius=1200};
  case 15:{_radius=5};
  case 25:{_radius=15};
  case 50:{_radius=25}; 
  case 75:{_radius=50};
  case 100:{_radius=75};
  case 250:{_radius=100};
  case 500:{_radius=250};
  case 800:{_radius=500}; 
  case 1200:{_radius=800};
  };
}
//More
else
{
  switch (radius) do
  {
  case 5:{_radius=15};
  case 15:{_radius=25};
  case 25:{_radius=50};
  case 50:{_radius=75};
  case 75:{_radius=100}; 
  case 100:{_radius=250};
  case 250:{_radius=500};
  case 500:{_radius=800};
  case 800:{_radius=1200}; 
  case 1200:{_radius=5};
  };
};
_radius;
};

vts_deletevehicle={
  _todel=_this select 0;
  _marker=_todel getVariable "vtsmarker";
  if !(isnil "_marker") then {deletemarkerlocal _marker;}:
  deleteVehicle _todel;
};

//Get current spawn setup
vts_storespawnsetup={
   
    _currentspawnsetup=[0,
                        From3D,
                        var_console_valid_side,
                        var_console_valid_camp,
                        var_console_valid_type,
                        console_unit_unite,
                        console_valid_unite,
                        var_console_valid_attitude,
                        var_console_valid_vitesse,
                        var_console_valid_mouvement,
                        console_unit_moral,
                        var_console_valid_formation,
                        round(console_unit_orientation),
                        radius,
                        [spawn_x,spawn_y,spawn_z],
                        [spawn_x2,spawn_y2,spawn_z2],
                        console_nom,
                        [console_init,"""",""""""]call KRON_Replace                                                
                        ];                     
  _currentspawnsetup;                        
}; 

//Restore a spawn setup (logically, not in the interface)
vts_restorespawnsetup={
  
  _cur=_this select 0;
  
  From3D=_cur select 1;publicvariable "From3D";
  var_console_valid_side=_cur select 2;publicvariable "var_console_valid_side";
  var_console_valid_camp=_cur select 3;publicvariable "var_console_valid_camp";
  var_console_valid_type=_cur select 4;publicvariable "var_console_valid_type";
  console_unit_unite=_cur select 5;publicvariable "console_unit_unite";
  console_valid_unite=_cur select 6;publicvariable "console_valid_unite";
  var_console_valid_attitude=_cur select 7;publicvariable "var_console_valid_attitude";
  var_console_valid_vitesse=_cur select 8;publicvariable "var_console_valid_vitesse";
  var_console_valid_mouvement=_cur select 9;publicvariable "var_console_valid_mouvement";
  console_unit_moral=_cur select 10;publicvariable "console_unit_moral";
  var_console_valid_formation=_cur select 11;publicvariable "var_console_valid_formation";
  console_unit_orientation=_cur select 12;publicvariable "console_unit_orientation";
  spawn_x=(_cur select 14) select 0;publicvariable "spawn_x";
  spawn_y=(_cur select 14) select 1;publicvariable "spawn_y";
  spawn_z=(_cur select 14) select 2;publicvariable "spawn_z";
  spawn_x2=(_cur select 15) select 0;publicvariable "spawn_x2";
  spawn_y2=(_cur select 15) select 1;publicvariable "spawn_y2";
  spawn_z2=(_cur select 15) select 2;publicvariable "spawn_z2";
  console_nom=_cur select 16;publicvariable "console_nom";
  console_init=[_cur select 17,"""""",""""] call KRON_Replace;publicvariable "console_init";
  
};

//Add action function
vts_AddAction={

  private ["_arguments","_priority","_showWindow","_hideOnUse","_shortcut","_condition"];
  _object=_this select 0;
  _action=_this select 1;
  _script=_this select 2;
  if (count _this > 3) then {_arguments = _this select 3};
  if (count _this > 4) then {_priority = _this select 4};
  if (count _this > 5) then {_showWindow = _this select 5};
  if (count _this > 6) then {_hideOnUse = _this select 6};
  if (count _this > 7) then {_shortcut = _this select 7};
  if (count _this > 8) then {_condition = _this select 8};
  if (isNil "_arguments") then {_arguments = []};
  if (isNil "_priority") then {_priority = 100};
  if (isNil "_showWindow") then {_showWindow = TRUE};
  if (isNil "_hideOnUse") then {_hideOnUse = TRUE};
  if (isNil "_shortcut") then {_shortcut = ""};
  if (isNil "_condition") then {_condition = "TRUE"};

  
  _action = _object addaction [_action,"scripts\VTS_Functions\vts_addaction.sqf",_arguments,_priority,_showWindow,_hideOnUse,_shortcut,_condition];
  _object setvariable [format["vtsaction_%1",_action],_script,true]; 

};
if (isserver) then {publicvariable "vts_AddAction";};

//Execute action function
vts_ExecuteAction={
  //hint format["%1",_this];
  _object=_this select 0;
  _caller=_this select 1;
  _id=_this select 2;
  
  _script=_object getVariable (format["vtsaction_%1",_id]);
  _this call _script; 
};

//Create a hostage behavior
vts_isHostagebehavior={

  _hostage=_this select 0;
  _loop=true;
  while {(!isnull _hostage) and (alive _hostage) and (_loop)} do
  {
  _hostage action ["Surrender",_hostage]; 
    {
       if (!isnil _x) then
       {
        call compile format["
        if (((_hostage knowsabout %1)>3) and (!captive %1) and ( (_hostage getVariable ""vts_unitside"") getFriend (side %1)>=0.6) ) then 
          {
            _hostage setcaptive false;
            _hostage enableai ""MOVE"";
            _newgroup = creategroup (side %1);
            [_hostage] joinsilent _newgroup;
            _loop=false;
            
          };
        ",_x];
      };
    } foreach player_units;
  //hint "hostage";
  sleep 5;
  };
 if ((alive _hostage) and (!isnull _hostage)) then {_hostage setBehaviour "COMBAT";};
};


vts_isHostage={

  _hostage=_this select 0;
  _typeof=typeof _hostage;
  if !(_typeof iskindof "Man") exitwith{};
  _hostage setVariable ["vts_unitside",side _hostage];
  _hostage allowFleeing 0;
  _hostage setcaptive true;
  removeAllWeapons _hostage;
  _hostage setBehaviour "CARELESS";
  _hostage addMPEventHandler ["MPkilled","if (player==user1) then {hintc format[""INFO : Hostage killed : %1 by %2 "",name (_this select 0),name (_this select 1)]} else {hint format[""%1 has been killed"",name (_this select 0)];};"];
  [_hostage,"""Follow Me""",{[(_this select 0)] joinsilent (_this select 1);(_this select 1) groupchat "Follow me";},[],100,true,true,"","alive _target"] call vts_addaction;
  [_hostage,"""Stay here""",{[(_this select 0)] joinsilent grpnull;(_this select 1) groupchat "Stay here";},[],100,true,true,"","alive _target"] call vts_addaction;
  _hostage disableAI "MOVE";
  //Behavior
  if (local _hostage) then {[_hostage] spawn vts_isHostagebehavior;};
};

if (isserver) then {publicvariable "vts_isHostage";};

//Create a IED behavior
vts_isIEDBehavior={

  _IED=_this select 0;
  _IED setvariable ["vts_IEDEnabled",true,true];
  _loop=true;
  _dist=7.5+random(30);
  while {(!isnull _IED) and (alive _IED) and (_loop)} do
  {
  
    {
       if (!isnil _x) then
       {
        call compile format["
        if (((speed vehicle %1)>15) and ((_IED distance vehicle %1)<_dist) and (!captive %1)) then 
          {
            [_IED] call vts_isIEDExplosion;
            _loop=false;
            
          };
        ",_x];
      };
    } foreach player_units;
  //hint "hostage";
  sleep 2.5;
  };
  
};


vts_isIEDExplosion={
  _IED=_this select 0;
  if (_IED getvariable "vts_IEDEnabled") then 
  {
    _IED setvariable ["vts_IEDEnabled",false,true];
    _bomb="R_57mm_HE";
    _bombrandom=random(10);
    if (_bombrandom>5) then {_bomb="R_57mm_HE";};
    if (_bombrandom>8) then {_bomb="R_80mm_HE";};
    _kaboom= _bomb createvehicle (getpos _IED);    
  };
};


vts_isIEDDisable={
  
  _IED=_this select 0;
  _IED removeAction (_this select 2);
  _target=_this select 1;
  _target action ["RepairVehicle",_target];
  if (_IED getvariable "vts_IEDEnabled") then
  {
    _IED setvariable ["vts_IEDEnabled",false,true];
    hint "Explosive device disabled";
    _script={};
    call compile format["_script={ if (player==user1) then {hintc ""INFO : %1 disarmed by %2"";}else {hint ""%1 : Explosive device disarmed"";}; };",typeof _IED,name _target];
    [_script] execvm "scripts\VTS_Functions\runcodeonallclients.sqf";
  }
  else
  {
    hint "Explosive device already disabled";
  };
  
};


vts_isIED={

  _IED=_this select 0;
  
  [_IED,"Disable explosive device",{_this call vts_isIEDDisable;},[],100,true,true,"","_target getvariable ""vts_IEDEnabled"""] call vts_addaction;
  //Behavior
  if (local _IED) then {
    [_IED] spawn vts_isIEDBehavior;
    _IED addEventHandler ["Killed","[(_this select 0)] call vts_isIEDExplosion;"];
  };  
  
};
if (isserver) then {publicvariable "vts_isIED";};

//Create Pickup Behavior
vts_isPickableGetit={

  _object=_this select 0;
  _target=_this select 1;
  _script={};
  hint format["%1 picked up",typeof _object];
  _target action ["RepairVehicle",_target];
  call compile format["_script={ if (player==user1) then {hintc ""INFO : %1 Picked up by %2"";} else {hint ""%1 picked up"";}; };",typeof _object,name _target];
  
  [_script] execvm "scripts\VTS_Functions\runcodeonallclients.sqf";
  deleteVehicle _object; 
  
};


vts_isPickable={

  _object=_this select 0;
  [_object,"Pickup object",{_this call vts_isPickableGetit;}] call vts_addaction;
};
if (isserver) then {publicvariable "vts_isPickable";};

//Create Target Behavior
vts_isTarget={
  
  _object=_this select 0;
  _object addMPEventHandler ["MPKilled"," if (player==user1) then {hintc format[""INFO : %1 Target destroyed by %2"",typeof (_this select 0),name (_this select 1)];} else {hint format[""%1 target destroyed"",typeof (_this select 0)];}; "];
};
if (isserver) then {publicvariable "vts_isTarget";};

//Create VIP Behavior
vts_isVIP={

  _object=_this select 0;
  _typeof=typeof _object;
   _object addMPEventHandler ["MPKilled","if (player==user1) then {hintc format[""INFO : %1 VIP Killed by %2 "",name (_this select 0),name (_this select 1)]} else {hint format[""%1 has been killed"",name (_this select 0)];};"];
   if (_typeof iskindof "Man")  then
   {
    [_object,"""Follow Me""",{[(_this select 0)] joinsilent (_this select 1);(_this select 1) groupchat "Follow me";},[],100,true,true,"","alive _target"] call vts_addaction;
    [_object,"""Stay here""",{[(_this select 0)] joinsilent grpnull;(_this select 1) groupchat "Stay here";},[],100,true,true,"","alive _target"] call vts_addaction;
   };

};
if (isserver) then {publicvariable "vts_isVIP";};





vts_isSniper={
  _sniper=_this select 0;
  _typeof=typeof _sniper;
  if !(_typeof iskindof "Man") exitwith{};
  null=[_sniper] execVM "scripts\CTB_Functions\sniperProne.sqf";
};
if (isserver) then {publicvariable "vts_isSniper";};



//Create Shop Behavior
vts_isShop={};
if (isserver) then {publicvariable "vts_isShop";};

//Get Cfgtype
vts_getClassCfgType={
  
  _class=_this select 0;
  _cfgtype="";
  
  if !(isnil "vts_shopcurrentconfig") exitwith {vts_shopcurrentconfig;};
  
  if (isclass (configFile >> "CfgVehicles" >> _class)) then {_cfgtype="CfgVehicles";};
  if (isclass (configFile >> "CfgAmmo" >> _class)) then {_cfgtype="CfgAmmo";};
  if (isclass (configFile >> "CfgMagazines" >> _class)) then {_cfgtype="CfgMagazines";};
  if (isclass (configFile >> "CfgWeapons" >> _class)) then {_cfgtype="CfgWeapons";}; 
  
  
  _cfgtype;
};

//Get Class CFG
vts_getClassCfg=
{
  _class=_this select 0;
  _classcfg=nil;
  _classtype=[_class] call vts_getClassCfgType;
  
  _classcfg=(configFile >> _classtype >> _class);
  
  _classcfg;
};

//GM ID helper
vts_showid={

  while {gps_eni_valid==1} do
  {
    _object=cursorTarget;
    if !(isnull _object) then
    {
      //titleText [format["%1",_object], "PLAIN",1];
      0 cutText["","PLAIN DOWN"];
      0 cutText [format["%1",_object],"PLAIN DOWN",0];
    };
    sleep 1;
  };  
  0 cutText["","PLAIN DOWN"];
};
